<?php

namespace App\Admin\Controllers;

use App\Admin\Exports\CoinLogExport;
use App\Http\Controllers\Controller;
use App\Model\CoinLog;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
// use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class CoinLogController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */

    public function index()
    {
        $this->adminId = Admin::user()->id;
        return Admin::content(function (Content $content) {
            $content->header('平台金币交易记录');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CoinLog());
        $where = [
            ['user_id', '>', 0],
            ['created_at', '>', date('Y-m-d', strtotime('-30 day'))]
        ];
        if (request('user_id')) {
            $where[] = ['flag', '<>', 3];
        }
        $grid->model()->where($where)->orderBy('id', 'desc');
        if ($this->adminId == 1) {
            $grid->id('记录ID')->sortable();
            $grid->order_id('订单号');
        }
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userProfile()->nick_name('昵称');
//        $grid->coin('交易金币')->totalRow(function ($amount) {
//            $amount = round($amount, 2);
//            return "<span class='text-danger text-bold'>金币总流水：{$amount}</span>";
//        });

        $grid->coin('交易金币');
        $grid->from('交易类型')->display(function ($data) {
            return CoinLog::FROM_DETAIL[$data];
        });

        if ($this->adminId == 1) {
            $grid->opProfile()->user_profile_id('交易对方ID');
            $grid->opProfile()->nick_name('交易对方昵称');
        }

        $grid->created_at('交易时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->remain_income_coin('交易后收入账户余额');
        $grid->remain_recharge_coin('交易后充值帐户余额');

        //禁用行选择
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableCreateButton();
        // $grid->disableExport();
        $grid->exporter(new CoinLogExport($grid));

        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('userProfile.user_profile_id', '用户ID');
                $filter->between('created_at', '交易时间')->datetime();
                $this->adminId == 1 && $filter->equal('log_type', '流水类型')->select(CoinLog::TYPE);

            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('op_user_id', '对方用户ID');
                $filter->equal('order_id', '订单号');
                $from = CoinLog::FROM_DETAIL;
                if ($this->adminId != 1) {
                    unset($from[CoinLog::FROM_RECHARGE]);
                    // unset($from[CoinLog::FROM_INVITE_RECHARGE]);
                    unset($from[CoinLog::FROM_INVITE_INCOME_CALL]);
                    unset($from[CoinLog::FROM_INVITE_INCOME_GIFT]);
                    unset($from[CoinLog::FROM_INVITE_PRIVATE_MSG]);
                    unset($from[CoinLog::FROM_INVITE_PRIVATE_MSG]);
                    unset($from[CoinLog::FROM_DAILY_FREE_RECHARGE]);
                    unset($from[CoinLog::FROM_DAILY_FREE]);
                }
                $filter->equal('from', '流水项目')->select($from);
            });
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableAll();
        });

        return $grid;
    }
}
